package simpletests;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.Test;

import voldemort.client.ClientConfig;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;
import voldemort.client.SocketStoreClientFactory;

public class ConnectPutAndGet {

	@SuppressWarnings("unchecked")
	@Test
	public void testSimplePutAndGet() {
		String bootstrapUrl = "tcp://localhost:6666";
		StoreClientFactory factory = new SocketStoreClientFactory(
				new ClientConfig().setBootstrapUrls(bootstrapUrl));

		StoreClient client = factory.getStoreClient("stressTestStore");
		
		

		client.put("Key1", "Value1");
		client.get("Key1");
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testSimplePutUsingMultipleThreads() throws IOException, InterruptedException, ExecutionException {
		String bootstrapUrl = "tcp://localhost:6666";
		
		ClientConfig clientConfig = new ClientConfig();
		clientConfig.setBootstrapUrls(bootstrapUrl);
		
		StoreClientFactory factory = new SocketStoreClientFactory(
				clientConfig);

		StoreClient client = factory.getStoreClient("stressTestStore");

		int totalNumberOfThreadsToLaunch = 200;
		int totalNumberOfKeysToGeneratePerThread = 100;

		List<PutABunchOfData> putABunchOfDataList = new ArrayList<PutABunchOfData>();
		List<Future> futureList = new ArrayList<Future>();
		
		
		
		for (int keyPrefix = 0; keyPrefix < totalNumberOfThreadsToLaunch; keyPrefix++) {
			PutABunchOfData putABunchOfData = new PutABunchOfData(keyPrefix,
					"SimpleMenuFile1.txt",
					totalNumberOfKeysToGeneratePerThread, client);
			putABunchOfData.delete();
			putABunchOfDataList.add(putABunchOfData);
		}

		ExecutorService threadExecutor = Executors
				.newFixedThreadPool(totalNumberOfThreadsToLaunch);
		
		for (int keyPrefix = 0; keyPrefix < totalNumberOfThreadsToLaunch; keyPrefix++) {
			Future future = threadExecutor.submit(putABunchOfDataList.get(keyPrefix));
			futureList.add(future);
		}
		
		for ( Future future : futureList)
		{
			future.get();
		}
		
		

	}
}


